import zipfile from '@feidao/web/atom/nodejs/zip/zip';
import { ICommonParams } from '@feidao/service/interfaces';
import http from 'http';

/**
 * 导出文件服务器上文件并压缩
 */

export default async function atom(msg: ICommonParams, action_id: string, session_id: string, spaceid: string) {
	// 接收传入的参数
	const info = msg.query;
	const id = '';
	const uri = `${info.file_url}getfile?productid=${spaceid}&id=${id}`;
	const res = await (() => {
		return new Promise<{ buff: Buffer; file: string; }>((resolve: any, reject: any) => {
			http.get(uri, (_res) => {
				const body: any[] = [];
				_res.on('error', (err) => {
					reject({});
				});
				_res.on('data', (ch) => {
					body.push(ch);
				});
				_res.on('end', (ch: any) => {
					resolve({ buf: Buffer.concat(body) });
				});
			});
		});
	})();
	const content = await zipfile({ file: res.buff, path: '下载文件名称.docx' });
	return {
		attachment: '压缩包名称.zip',
		buff: content,
		content_type: 'application/octet-stream'
	};
}
